using System;
using MonoDevelop.ResourceEditor;

namespace MonoDevelop.ResourceEditor.Gui
{
	
	public class BooleanView : Gtk.HBox, IResourceView
	{
		#region Child Widgets
		protected Gtk.RadioButton rbnTrue;
		protected Gtk.RadioButton rbnFalse;
		#endregion
		
		#region Events
		/// <summary>Event is raised when the value of the resource has been changed</summary>
		public event ResourceChangedEventHandler ResourceChanged;
		#endregion
		
		#region Properties
		/// <summary>Gets or sets whether or not the current view is write protected or not</summary>
		/// <value>True if the widget is write protected, otherwise flase</value>
		public bool WriteProtected
		{
			get { return !this.rbnTrue.CanFocus; }
			set 
			{
				this.rbnTrue.CanFocus = !value;
				this.rbnFalse.CanFocus = !value;
			}
		}
		
		/// <summary>Gets or sets the resource item for the widget</summary>
		/// <value>A ResourceItem containing the name and value for the resource</value>
		public ResourceItem ResourceItem
		{
			get { return resourceItem; }
			set 
			{
				resourceItem = value;
				this.rbnTrue.Active = (bool) value.ResourceValue;
				this.rbnFalse.Active = !(bool)value.ResourceValue;
			}
		}
		#endregion
		
		#region Member Variables
		/// <summary>Holds the value for the ResourceItem property</summary>
		private ResourceItem resourceItem;
		#endregion
		
		/// <summary>Initializes a new instance of the TextView widget by specifying the ResourceItem</summary>
		/// <param name="item">A ResourceItem containing the value to display</param> 
		public BooleanView(ResourceItem item)
		{
			this.BorderWidth = 7;
			// True radio button
			this.rbnTrue = new Gtk.RadioButton(null, "True");
			this.PackStart(this.rbnTrue, true, true, 0);
			this.rbnTrue.Show();
			this.rbnTrue.Clicked += new EventHandler(GroupButton_Clicked);
			
			// False radio button
			this.rbnFalse = new Gtk.RadioButton(this.rbnTrue, "False");
			this.PackStart(this.rbnFalse, true, true, 0);
			this.rbnFalse.Show();
			
			this.ResourceItem = item;
		}
		
		/// <summary>Execute the event when the resource value has been changed</summary>
		/// <param name="sender">An object that contains the sender data.</param>
		/// <param name="e">An EventArgs object that contains the event data.</param>
		private void GroupButton_Clicked(object sender, EventArgs e)
		{
			if(ResourceChanged != null) 
			{
				ResourceChanged(this, new ResourceEventArgs(resourceItem.Name, rbnTrue.Active));
			}
		}
	}	
}
